package com.lanyun.core.encrypt.handler;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @ClassName：Base64EncryptHandler
 * @Description: base64加密解密
 * @Author: Jqcc
 * @Date: 2025/3/13
 */
public class Base64EncryptHandler implements EncryptHandler {
    @Override
    public String encrypt(String content) {
        try {
            return Base64.getEncoder().encodeToString(content.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }

    @Override
    public String decrypt(String content) {
        try {
            byte[] asBytes = Base64.getDecoder().decode(content);
            return new String(asBytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }

    public static void main(String[] args) {
        Base64EncryptHandler handler = new Base64EncryptHandler();
        System.out.println(handler.encrypt("123456"));
        System.out.println(handler.decrypt(handler.encrypt("123456")));
    }

}
